﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedGenomics.Domain
{
    [Flags]
    public enum ModelOptionFlags : byte
    {
        None = 0x0,
        Alleleic = 0x1,
        Dominant = 0x2,
        Trend = 0x4,
        Recessive = 0x8,
        Genotypic = 0x10,
        All = 0x1F
    }

    public static class ModelOptionFlagExtensions
    {
        public static bool IsAllelicSet(this ModelOptionFlags flags)
        {
            return IsSet(ModelOptionFlags.Alleleic, flags);
        }

        public static ModelOptionFlags SetAllelic(this ModelOptionFlags flags, bool set)
        {
            return Set(flags, ModelOptionFlags.Alleleic, set);
        }

        public static bool IsDominantSet(this ModelOptionFlags flags)
        {
            return IsSet(ModelOptionFlags.Dominant, flags);
        }

        public static ModelOptionFlags SetDominant(this ModelOptionFlags flags, bool set)
        {
            return Set(flags, ModelOptionFlags.Dominant, set);
        }

        public static bool IsTrendSet(this ModelOptionFlags flags)
        {
            return IsSet(ModelOptionFlags.Trend, flags);
        }

        public static ModelOptionFlags SetTrend(this ModelOptionFlags flags, bool set)
        {
            return Set(flags, ModelOptionFlags.Trend, set);
        }

        public static bool IsRecessiveSet(this ModelOptionFlags flags)
        {
            return IsSet(ModelOptionFlags.Recessive, flags);
        }

        public static ModelOptionFlags SetRecessive(this ModelOptionFlags flags, bool set)
        {
            return Set(flags, ModelOptionFlags.Recessive, set);
        }

        public static bool IsGenotypicSet(this ModelOptionFlags flags)
        {
            return IsSet(ModelOptionFlags.Genotypic, flags);
        }

        public static ModelOptionFlags SetGenotypic(this ModelOptionFlags flags, bool set)
        {
            return Set(flags, ModelOptionFlags.Genotypic, set);
        }

        private static bool IsSet(ModelOptionFlags flag, ModelOptionFlags flags)
        {
            return (flags & flag) != ModelOptionFlags.None;
        }

        private static ModelOptionFlags Set(ModelOptionFlags flags, ModelOptionFlags flagToSet, bool set)
        {
            return set ? flags | flagToSet : flags ^ flagToSet;
        }
    }
}
